import { axios } from "../../utils/request";
Page({
  /**
   * 页面的初始数据
   */
  data: {},
  login() {
    return new Promise((resolve, reject) => {
      wx.login({
        success(res) {
          resolve(res);
        },
        fail(err) {
          reject(err);
        },
      });
    });
  },
  async getUserInfo() {
    try {
      const getInfo = () => wx.getUserProfile({ desc: "获取信息" });
      wx.showLoading({ title: "登录中", mask: true });
      const [info, loginData] = await Promise.all([getInfo(), this.login()]);
      let UserData = { code: (loginData as any).code, ...info.userInfo };
      let res = (await axios("/user", "POST", { ...UserData })) as Response;
      const { token } = res.data as any;
      if (res.statusCode == 200) {
        wx.setStorageSync("token", token);
        wx.setStorageSync("userInfo", info.userInfo);
        wx.setStorageSync("isLogin", true);
        this.setData({
          userInfo: info.userInfo,
        });
        setTimeout(function () {
          wx.hideLoading();
        }, 200);
        wx.showToast({ title: "登录成功", icon: "success" });
        wx.navigateBack();
      }
    } catch (error) {
      console.log(error);
    }
  },
});
